package cn.rkylin.oms.system.splitRule.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageInfo;

import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.item.service.IItemService;
import cn.rkylin.oms.item.vo.ItemVO;
import cn.rkylin.oms.system.shop.service.IShopService;
import cn.rkylin.oms.system.shop.vo.ShopVO;
import cn.rkylin.oms.system.splitRule.dao.ISplitRuleItemDAO;
import cn.rkylin.oms.system.splitRule.domain.SplitRuleItem;
import cn.rkylin.oms.system.splitRule.vo.SplitRuleItemVO;
import cn.rkylin.oms.system.splitRule.vo.SplitRuleVO;

@Service("splitRuleItemService")
public class SplitRuleItemServiceImpl extends ApolloService implements ISplitRuleItemService {
	
	private static final String RESULT_SUCCESS_FLG = "success";  
	private static final String RESULT_ERROR_MESSAGE = "errormessage";
	private static final String RESULT_ROW_COUNT = "rowcount";
	private static final String RESULT_SPLIT_RULE_ID = "splitruleid";
	
	@Resource(name = "redisTemplate")
	private RedisTemplate<Serializable, Serializable> redisTemplate;
	/**
     * 平台商品服务层
     */
    @Autowired
    private IItemService itemService;
    
	/**
     * 分单规则服务层
     */
    @Autowired
    private ISplitRuleService splitRuleService;
    
	/**
     * 店铺服务层
     */
    @Autowired
    private IShopService shopService;
    
	/**
	 * 分单规则数据访问
	 */
	@Autowired
	private ISplitRuleItemDAO splitRuleItemDAO;

	/**
	 * 构造函数
	 */
	public SplitRuleItemServiceImpl(){

	}
	
	/**
	 * 查询分单规则
	 * 
	 * @param SplitRuleVO
	 */
	public PageInfo<SplitRuleItemVO> findByWhere(int page, int rows, SplitRuleItemVO splitRuleItemVO) throws Exception {
		PageInfo<SplitRuleItemVO> SplitRuleItemVOList = findPage(page, rows, "pageSelectSplitRuleItem", splitRuleItemVO);
		return SplitRuleItemVOList;
	}

	/**
	 * 查询分单规则
	 * 
	 * @param SplitRuleVO
	 */
	public PageInfo<SplitRuleItemVO> getSplitRultItemList(int page, int rows, SplitRuleItemVO splitRuleItemVO) throws Exception {
		PageInfo<SplitRuleItemVO> SplitRuleItemVOList = findPage(page, rows, "pageSelectSplitRuleItemList", splitRuleItemVO);
		return SplitRuleItemVOList;
	}
	/**
	 * 新规分单规则详情
	 */
	@Override
	@Transactional
	@CachePut(value="splitRuleItem", key="T(String).valueOf('splitRule:').concat(#splitruleid)", unless="#result == null")
	public List<SplitRuleItem> insert(List<SplitRuleItemVO> list, String splitruleid) throws Exception {
		List<SplitRuleItem> listSplitRuleItem = new ArrayList<SplitRuleItem>();
		for (int i = 0 ; i < list.size() ; i++){
			SplitRuleItem splitRuleItem = new SplitRuleItem();
			BeanUtils.copyProperties(list.get(i), splitRuleItem);
			splitRuleItemDAO.insert(splitRuleItem);
			listSplitRuleItem.add(splitRuleItem);
		}
		List<SplitRuleItem> listSplitItem = (List<SplitRuleItem>) redisTemplate.opsForValue().get("splitRule:"+splitruleid);
		if (listSplitItem!=null && listSplitItem.size()>0){
			listSplitRuleItem.addAll(listSplitItem);
		}
		return listSplitRuleItem;
	}

	@Override
	public Map<String, Object> update(SplitRuleItemVO splitRuleItemVO) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Map<String , Object> getListSplitRuleItem(String splitruleid , String ecitemid , String splitShopId){
		Map<String , Object> resultMap = new HashMap<String , Object>();
		try {
			//分单Id空的场合
	  		if (StringUtils.isBlank(splitruleid)){
	  			resultMap.put(RESULT_SUCCESS_FLG,"N");
	          	resultMap.put(RESULT_ERROR_MESSAGE,"所选择的的分单id是空");
	          	resultMap.put(RESULT_ROW_COUNT,"0");
	          	return resultMap;
	  		}
	  		
	  		//分单规则id在数据库不存场合	
	  		SplitRuleVO splitRuleVO = new SplitRuleVO();
	  		splitRuleVO.setSplitRuleId(splitruleid);
	  		PageInfo<SplitRuleVO> splitRuleVOList = splitRuleService.findByWhere(1, 1, splitRuleVO);
	  		if (splitRuleVOList.getList()==null || splitRuleVOList.getList().size() <=0){
	          	resultMap.put(RESULT_SUCCESS_FLG,"N");
	          	resultMap.put(RESULT_ERROR_MESSAGE,"所选择的的分单规则已经不存在");
	          	resultMap.put(RESULT_ROW_COUNT,"0");
	          	return resultMap;
	          }
	  		
	  		//分单店铺id空场合
	  		if (StringUtils.isBlank(splitShopId)){
	  			resultMap.put(RESULT_SUCCESS_FLG,"N");
	          	resultMap.put(RESULT_ERROR_MESSAGE,"所选择的的店铺id是空");
	          	resultMap.put(RESULT_ROW_COUNT,"0");
	          	return resultMap;
	  		}
	  		
	  		//分单店铺id在数据库不存场合		
	  		ShopVO paramVO = new ShopVO();
		      paramVO.setShopId(splitShopId);
		      PageInfo<ShopVO> shopList = shopService.findByWhere(1, 1, paramVO);
		      if (shopList.getList()==null || shopList.getList().size() <=0){
		      	resultMap.put(RESULT_SUCCESS_FLG,"N");
		      	resultMap.put(RESULT_ERROR_MESSAGE,"所选择的的店铺已经不存在");
		      	resultMap.put(RESULT_ROW_COUNT,"0");
		      	return resultMap;
		      }
		      
			//商品id是空的场合
			if (StringUtils.isBlank(ecitemid)){
				resultMap.put(RESULT_SUCCESS_FLG,"N");
			  	resultMap.put(RESULT_ERROR_MESSAGE,"所选择的的商品id是空");
			  	resultMap.put(RESULT_ROW_COUNT,"0");
			  	return resultMap;
			}
	      	
			String[] itemTemp = ecitemid.split(",");
			String splitRuleItemId = "";
			List<SplitRuleItemVO> list = new ArrayList<SplitRuleItemVO>();
			for (int i = 0 ; i < itemTemp.length ; i++){
				
				if (StringUtils.isBlank(itemTemp[i])){
					resultMap.put(RESULT_SUCCESS_FLG,"N");
		        	resultMap.put(RESULT_ERROR_MESSAGE,"所选择的的商品id是空");
		        	resultMap.put(RESULT_ROW_COUNT,"0");
		        	return resultMap;
				}
				
				
				//商品id在数据库不存场合
				ItemVO itemVO = new ItemVO();
				itemVO.setEcItemId(itemTemp[i]);
				PageInfo<ItemVO> itemVOList = itemService.findByWhere(1, 1, itemVO);
				if (itemVOList.getList()==null || itemVOList.getList().size() <=0){
		        	resultMap.put(RESULT_SUCCESS_FLG,"N");
		        	resultMap.put(RESULT_ERROR_MESSAGE,"所选择的的商品已经不存在");
		        	resultMap.put(RESULT_ROW_COUNT,"0");
		        	return resultMap;
		        }
				
				//同一个分担规则，同一个商品在分担规则详情页面只能有一条数据
				SplitRuleItemVO checkVO = new SplitRuleItemVO();
				checkVO.setEcItemId(itemTemp[i]);
				checkVO.setSplitRuleId(splitruleid);
				PageInfo<SplitRuleItemVO> checkVOList = this.findByWhere(1, 1, checkVO);
				if (checkVOList.getList()==null || checkVOList.getList().size() >0){
					resultMap.put(RESULT_SUCCESS_FLG,"N");
					resultMap.put(RESULT_ERROR_MESSAGE,"当前分担规则下的商品已经存在！");
					resultMap.put(RESULT_ROW_COUNT,"0");
					return resultMap;
				}
			      
		  		SplitRuleItemVO splitRuleItemVO = new SplitRuleItemVO();
		  		splitRuleItemVO.setSplitRuleId(splitruleid);
		        splitRuleItemVO.setSplitShopId(splitShopId);
		        splitRuleItemVO.setCreateTime(new Date());
		        splitRuleItemVO.setUpdateTime(new Date());
		        splitRuleItemVO.setDeleted("n");
		    	splitRuleItemVO.setSplitShopName(shopList.getList().get(0).getShopName());
		    	splitRuleItemVO.setEcItemId(itemTemp[i]);
				splitRuleItemVO.setEcItemCode(itemVOList.getList().get(0).getEcItemCode());
				splitRuleItemVO.setSplitRuleItemId(java.util.UUID.randomUUID().toString().replaceAll("-", ""));
				
				list.add(splitRuleItemVO);
//		  		splitRuleItemService.insert(splitRuleItemVO);
				
				if (i==0){
					splitRuleItemId = splitRuleItemVO.getSplitRuleItemId();
				} else {
					splitRuleItemId = splitRuleItemId + "," + splitRuleItemVO.getSplitRuleItemId();
				}
			}
			resultMap.put(RESULT_SUCCESS_FLG,"Y");
			resultMap.put(RESULT_SPLIT_RULE_ID, splitRuleItemId);
			resultMap.put(RESULT_ERROR_MESSAGE,"");
			resultMap.put("list", list);
		} catch (Exception e){
			resultMap.put(RESULT_SUCCESS_FLG,"N");
			resultMap.put(RESULT_ERROR_MESSAGE,e.getMessage());
			resultMap.put(RESULT_ROW_COUNT,"0");
		}
		
		return resultMap;
	}
}
